#include <boost/log/core.hpp>
#include <boost/log/trivial.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/sinks/text_file_backend.hpp> // 如果使用文本文件sink
#include <boost/log/utility/setup/file.hpp>      // 用于设置文件日志sink
namespace logging = boost::log;
namespace expr = boost::log::expressions;
namespace sinks = boost::log::sinks;
namespace keywords =  boost::log::keywords ;
void init_logging() {
    // 创建一个文本文件sink
    typedef sinks::synchronous_sink<sinks::text_file_backend> sink_t;
    boost::shared_ptr<sink_t> sink(new sink_t(
        keywords::file_name = "sample_%N.log",   // 文件名格式，%N表示序号
        keywords::rotation_size = 10 * 1024 * 1024, // 文件大小达到10MB时滚动
        keywords::time_based_rotation = sinks::file::rotation_at_time_point(0, 0, 0) // 每天0点滚动
    ));

    // 设置sink的格式化器
    sink->set_formatter(expr::format("%1%: [%2%] - %3%")
        % expr::attr<boost::posix_time::ptime>("TimeStamp")
        % logging::trivial::severity
        % expr::smessage);

    // 添加sink到核心logger
    logging::core::get()->add_sink(sink);

    // 设置日志级别过滤
    logging::core::get()->set_filter(
        logging::trivial::severity >= logging::trivial::info
    );
}

int main() {
    init_logging(); // 初始化日志系统

    BOOST_LOG_TRIVIAL(debug) << "This is a debug message"; // 不会被记录，因为默认级别是info及以上
    BOOST_LOG_TRIVIAL(info) << "Application started"; // 会被记录
    BOOST_LOG_TRIVIAL(warning) << "Warning: Something might be wrong"; // 会被记录
    BOOST_LOG_TRIVIAL(error) << "Error occurred!"; // 也会被记录

    return 0;
}